import { objectAny } from "../../options/base";
import { mySchoolStore } from "../../store";
import { showModal, showToast } from "../../utils/utils";
import { orderTypeRule } from "../utils";

// @ts-ignore
const riderOrderMange=uniCloud.importObject(`rider-order-mange`,{customUI: true})

// 获取对应状态的订单列表
export async function db_riderQueryOrderList(
    orderType?:orderTypeRule,
    orderId?:string
){
    const schoolStore = mySchoolStore();
    try {
        const {data,myErrCode,errMsg} = await riderOrderMange.riderQueryOrderList(orderType,schoolStore.mySchool._id,orderId);

        if(myErrCode!==0){
            showModal({
                title:`异常提示`,
                content:errMsg,
                showCancel:false
            })
            return []
        }
        const returnList=data.all_list.map((it:objectAny)=>{
            it.current_loca_key = `${orderType}_${it.order_status}_${it._id}`
            return it
        })
        return returnList;
    } catch (error) {
        console.error(`xxxxxx=> 调用 db_riderQueryOrderList 错误，错误信息如下： <=xxxxxx`)
        console.error(error)
        showModal({
            title:`异常提示`,
            content:`访问过于火爆，请稍后再试`,
            showCancel:false
        }) 
    }

    return []
}


export async function db_startGrabOrder(
    dataId:string,
    orderId:string
):Promise<boolean>{
    const schoolStore = mySchoolStore();
    try {
        uni.showLoading({
            title: `正在抢单`,
            mask:true
        });
        const {data,myErrCode,errMsg} = await riderOrderMange.startGrabOrder(schoolStore.mySchool._id,dataId,orderId);
        uni.hideLoading();
        if(myErrCode!==0){
            if(myErrCode!==10004){
                showModal({
                    title:`异常提示`,
                    content:errMsg,
                    showCancel:false
                })
                return false
            }else{
                showToast(errMsg,`none`)
            }
        }else{
            showToast(`抢单成功`,`success`,false,1000)
        }
        return true
    } catch (error) {
        uni.hideLoading();
        console.error(`xxxxxx=> 调用 db_startGrabOrder 错误，错误信息如下： <=xxxxxx`)
        console.error(error)
        showModal({
            title:`异常提示`,
            content:`访问过于火爆，请稍后再试`,
            showCancel:false
        }) 
    }

    return false
}

export async function db_submitSelfOrder(
    dataId:string,
    orderId:string,
    orderStatus:3|4,
    extData:objectAny | undefined = {},
    prompt:boolean|undefined =true,
):Promise<boolean>{
    const schoolStore = mySchoolStore();
    try {
        if(prompt){
            uni.showLoading({
                title: `正在提交`,
                mask:true
            });
        }
        const {data,myErrCode,errMsg} = await riderOrderMange.submitOrderStatus(schoolStore.mySchool._id,dataId,orderId,orderStatus,extData);
        if(prompt){ uni.hideLoading(); }
        if(myErrCode!==0){
            if(myErrCode!==10020){
                showModal({
                    title:`异常提示`,
                    content:errMsg,
                    showCancel:false
                })
                return false
            }else{
                showToast(errMsg,`none`)
            }
        }else{
            showToast(`提交成功`,`success`,false,1000)
        }
        return true
    } catch (error) {
        if(prompt){ uni.hideLoading(); }
        console.error(`xxxxxx=> 调用 db_submitSelfOrder 错误，错误信息如下： <=xxxxxx`)
        console.error(error)
        showModal({
            title:`异常提示`,
            content:`访问过于火爆，请稍后再试`,
            showCancel:false
        }) 
    }
    return false
}


export async function db_riderTurnOrder(
    dataId:string,
    orderId:string,
    toUserId:string
):Promise<boolean>{
    const schoolStore = mySchoolStore();
    const schoolId = schoolStore.mySchool._id
    try {
        uni.showLoading({
            title: `正在提交`,
            mask:true
        });
        const {data,myErrCode,errMsg} = await riderOrderMange.riderTurnOrder(schoolId,dataId,orderId,toUserId);
        uni.hideLoading();
        if(myErrCode!==0){
            if(myErrCode === 10006 || myErrCode === 10005 || myErrCode === 10003){
                showToast(errMsg,`none`)
            }else{
                showModal({
                    title:`异常提示`,
                    content:errMsg,
                    showCancel:false
                })
                return false
            }
        }else{
            showToast(`转单成功`,`success`,false,1000)
        }
        return true
    } catch (error) {
        uni.hideLoading();
        console.error(`xxxxxx=> 调用 db_riderTurnOrder 错误，错误信息如下： <=xxxxxx`)
        console.error(error)
        showModal({
            title:`异常提示`,
            content:`访问过于火爆，请稍后再试`,
            showCancel:false
        }) 
    }
    return false
}